<?php if ($employmentContractPermissions->canCreate()) : ?>

    <?php use_javascripts_for_form($form); ?>
    <?php use_stylesheets_for_form($form); ?>

    <div class="box pimPane">

        <?php
        include_partial('pim/pimLeftMenu', array('empNumber' => $empNumber, 'form' => $form));
        ?>

        <div class="saveEmploymentContract">
            <div class="head">
                <?php $heading = (empty($form->employmentContractId)) ? __("Add Employment Contract") : __("Edit Employment Contract") ?>
                <h1 id="saveEmploymentContract"><?php echo $heading; ?></h1>
            </div>
            <!-- head -->

            <div class="inner">

                <?php if ($employmentContractPermissions->canRead()) : ?>
                    <?php include_partial('global/flash_messages', array('prefix' => 'employmentcontract')); ?>

                    <form name="frmSaveEmploymentContract" id="frmSaveEmploymentContract" method="post"
                          action="<?php echo url_for('tlupim/saveEmploymentContract?empNumber=' . $empNumber . ((!empty($form->employmentContractId)) ? '&employmentContractId=' . $form->employmentContractId : '')); ?>"
                          class="<?php echo empty($form->employmentContractId) ? '' : 'clickToEditForm'; ?>">

                        <?php echo $form['_csrf_token']; ?>
                        <?php echo $form['empNumber']->render(); ?>

                        <fieldset>
                            <ol>
                                <li>
                                    <?php echo $form['empContractNo']->renderLabel(__('Employment Contract Number') . ' <em>*</em>'); ?>
                                    <?php echo $form['empContractNo']->render(array("class" => "formInputText", "maxlength" => 10)); ?>
                                </li>
                                <li>
                                    <?php echo $form['employment_contract_type']->renderLabel(__('Employment Contract Type') . ' <em>*</em>'); ?>
                                    <?php echo $form['employment_contract_type']->render(array("class" => "formSelect")); ?>
                                </li>
                                <li>
                                    <?php echo $form['duration']->renderLabel(__('Employment Contract Duration') . ' <em>*</em>'); ?>
                                    <?php echo $form['duration']->render(array("class" => "formInputText", "maxlength" => 30)); ?>
                                </li>
                                <li>
                                    <?php echo $form['salary']->renderLabel(__('Salary') . ' <em>*</em>'); ?>
                                    <?php echo $form['salary']->render(array("class" => "formInputText", "maxlength" => 50)); ?>
                                </li>
                                <li>
                                    <?php echo $form['start_date']->renderLabel(__('Employment Contract Start Date')); ?>
                                    <?php echo $form['start_date']->render(array("class" => "formDateInput")); ?>
                                </li>
                                <li>
                                    <?php echo $form['end_date']->renderLabel(__('Employment Contract End Date')); ?>
                                    <?php echo $form['end_date']->render(array("class" => "formDateInput")); ?>
                                </li>
                                <li>
                                    <?php echo $form['comment']->renderLabel(__('Employment Contract Comment')); ?>
                                    <?php echo $form['comment']->render(array("class" => "formInputText", "maxlength" => 255)); ?>
                                </li>
                                <li class="required">
                                    <em>*</em> <?php echo __(CommonMessages::REQUIRED_FIELD); ?>
                                </li>
                            </ol>

                            <p>
                                <?php if ($employmentContractPermissions->canUpdate()): ?>
                                    <input type="button" class="editButton" name="btnSave" id="btnSave"
                                           value="<?php echo empty($form->employmentContractId) ? __("Save") : __("Edit"); ?>"/>
                                    <input type="button" class="reset" name="btnCancel" id="btnCancel"
                                           value="<?php echo __("Cancel"); ?>"/>
                                <?php endif; ?>
                                <input type="button" class="backbutton" name="btnBack" id="btnBack"
                                       value="<?php echo __("Back"); ?>"/>
                            </p>
                        </fieldset>
                    </form>
                <?php else: ?>
                    <div><?php echo __(CommonMessages::DONT_HAVE_ACCESS); ?></div>
                <?php endif; ?>

            </div>
            <!-- inner -->
        </div>

        <?php
        if (!empty($form->employmentContractId)) {
            echo include_component('pim', 'attachments', array('empNumber' => $empNumber, 'screen' => EmployeeAttachment::SCREEN_EMPLOYMENT_CONTRACT));
        }
        ?>

    </div>

    <script type="text/javascript">
        //<![CDATA[

        var lang_edit = "<?php echo __("Edit"); ?>";
        var lang_save = "<?php echo __("Save"); ?>";
        var viewEmploymentContractListUrl = '<?php echo url_for('tlupim/viewEmploymentContractList?empNumber=' . $empNumber); ?>';
        var employmentContractId = '<?php echo $form->employmentContractId; ?>';
        var lang_invalidDate = '<?php echo __(ValidationMessages::DATE_FORMAT_INVALID, array('%format%' => str_replace('yy', 'yyyy', get_datepicker_date_format($sf_user->getDateFormat())))) ?>'
        var lang_startDateAfterEndDate = "<?php echo __('End date should be after start date'); ?>";
        var lang_empContractNoRequired = '<?php echo __(ValidationMessages::REQUIRED); ?>';
        var lang_employmentContractTypeRequired = '<?php echo __(ValidationMessages::REQUIRED); ?>';
        var lang_salaryRequired = '<?php echo __(ValidationMessages::REQUIRED); ?>';
        var lang_exceed10Chars = '<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 10)); ?>';
        var lang_exceed30Chars = '<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 30)); ?>';
        var lang_exceed255Chars = '<?php echo __(ValidationMessages::TEXT_LENGTH_EXCEEDS, array('%amount%' => 255)); ?>';
        var lang_salaryRange = "<?php echo __('Salary must in range from 0 to 999999999.99'); ?>";
        var datepickerDateFormat = '<?php echo get_datepicker_date_format($sf_user->getDateFormat()); ?>';

        //]]>
    </script>

    <script type="text/javascript">
        //<![CDATA[

        var stratDate = "";

        function clearErrors() {
            $('#frmSaveEmploymentContract').validate().resetForm();
        }

        $(document).ready(function () {

            $("#btnCancel").click(function () {
                clearErrors();
            });

            $("#btnBack").click(function () {
                window.location.replace(viewEmploymentContractListUrl);
            })

            $("#frmSaveEmploymentContract").validate({
                rules: {
                    'employmentContract[empContractNo]': { required: true, maxlength: 10 },
                    'employmentContract[employment_contract_type]': { required: true },
                    'employmentContract[duration]': { required: false, maxlength: 30 },
                    'employmentContract[salary]': { required: true, range: [0, 999999999.99] },
                    'employmentContract[start_date]': { required: false, valid_date: function () {
                        return {format: datepickerDateFormat, required: false, displayFormat: displayDateFormat}
                    }},
                    'employmentContract[end_date]': { required: false, valid_date: function () {
                        return {format: datepickerDateFormat, required: false, displayFormat: displayDateFormat}
                    }, date_range: function () {
                        return {format: datepickerDateFormat, displayFormat: displayDateFormat, fromDate: stratDate}
                    } },
                    'employmentContract[comment]': { required: false, maxlength: 255 }
                },
                messages: {
                    'employmentContract[empContractNo]': {
                        required: lang_empContractNoRequired,
                        maxlength: lang_exceed10Chars
                    },
                    'employmentContract[employment_contract_type]': {
                        required: lang_employmentContractTypeRequired
                    },
                    'employmentContract[duration]': { maxlength: lang_exceed30Chars },
                    'employmentContract[salary]': {
                        required: lang_salaryRequired,
                        range: lang_salaryRange
                    },
                    'employmentContract[start_date]': { valid_date: lang_invalidDate },
                    'employmentContract[end_date]': { valid_date: lang_invalidDate, date_range: lang_startDateAfterEndDate },
                    'employmentContract[comment]': { maxlength: lang_exceed255Chars }
                }
            });

            var list = new Array(
                '#employmentContract_empContractNo', '#employmentContract_employment_contract_type',
                'employmentContract_durationy', '#employmentContract_salary', '#start_date',
                '#end_date', '#employmentContract_comment'
            );

            if (employmentContractId != "") {
                for (var i = 0; i < list.length; i++) {
                    $(list[i]).attr("disabled", "disabled");
                }

                $("#start_date,#end_date").datepicker('disable');
            }

            $("#btnSave").click(function () {
                //if user clicks on Edit make all fields editable
                if ($("#btnSave").attr('value') == lang_edit) {
                    for (i = 0; i < list.length; i++) {
                        $(list[i]).removeAttr("disabled");
                    }
                    $("#start_date,#end_date").datepicker('enable');
                    $("#btnSave").attr('value', lang_save);
                    return;
                }

                if ($("#btnSave").attr('value') == lang_save) {
                    $("#frmSaveEmploymentContract").submit();
                }
            });
        });
        //]]>
    </script>

<?php endif; ?>